import 'package:dio/dio.dart';

import '../../config/app_glabol.dart';
import '../other/log/log.dart';

///默认的网络拦截器
class DefaultInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    LogUtils.e("请求开始 ${options.method} : ${options.baseUrl}${options.path}");
    options.headers['utm'] = AppGlabol.channel;
    super.onRequest(options, handler);
  }

  @override
  Future onResponse(
      Response response, ResponseInterceptorHandler handler) async {
    try {
      LogUtils.e(
          '接口 ${response.requestOptions.method} : ${response.requestOptions.baseUrl}${response.requestOptions.path}');
      if (response.requestOptions.method == "POST") {
        LogUtils.e('参数 : ${response.requestOptions.data}');
      } else {
        LogUtils.e('参数 : ${response.requestOptions.queryParameters}');
      }
      LogUtils.e('返回数据: ${response.data}');
      LogUtils.e('\n');
    } catch (e) {
      return response;
    }
    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    // TODO: implement onError
    LogUtils.e('\n');
    LogUtils.e(err);
    LogUtils.e('\n');
    LogUtils.e('报错接口: ${err.requestOptions.baseUrl}${err.requestOptions.path}');
    if (err.requestOptions.method == "POST") {
      LogUtils.e('参数: ${err.requestOptions.data}');
    } else {
      LogUtils.e('参数: ${err.requestOptions.queryParameters}');
    }
    super.onError(err, handler);
  }
}
